<template>
  <div class="system-user-dialog-container">
    <el-dialog
      :title="state.dialog.title"
      v-model="state.dialog.isShowDialog"
      width="769px"
    >
      <el-form
        ref="companyDialogFormRef"
        :rules="rules"
        :model="state.ruleForm"
        size="default"
        label-width="90px"
      >
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="公司名称" prop="name">
              <el-input
                v-model="state.ruleForm.name"
                placeholder="请输入公司名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="公司电话" prop="tel">
              <el-input
                v-model="state.ruleForm.tel"
                placeholder="请输入公司电话"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="公司地址" prop="address">
              <el-input
                v-model="state.ruleForm.address"
                placeholder="请输入公司地址"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
            <el-form-item label="公司金额" prop="money">
              <el-input
                v-model="state.ruleForm.money"
                placeholder="请输入金额"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
            <el-form-item label="备注" prop="note">
              <el-input
                v-model="state.ruleForm.note"
                type="textarea"
                placeholder="请输入备注"
                maxlength="150"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default">{{
            state.dialog.submitTxt
          }}</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref } from "vue";
import { update, add } from "/@/api/company/index";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormInstance, FormRules } from 'element-plus'

// 定义子组件向父组件传值/事件
interface RuleForm {
  name: string
  tel: string
}
// 定义子组件向父组件传值/事件
const emit = defineEmits(["refresh"]);

// 定义变量内容
const companyDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: "",
    name: "", //
    money: "0", //
    note: "", //
    tel: "", //
    address: "",
  },
  dialog: {
    isShowDialog: false,
    type: "",
    title: "",
    submitTxt: "",
  },
});
const rules = reactive<FormRules<RuleForm>>({
  name: [{ trigger: "blur" }],
  tel: [{ trigger: "blur" }],
});
// 打开弹窗
const openDialog = (type: string, row: any) => {
  state.dialog.type = type;
  if (type === "edit") {
    state.ruleForm = row;
    state.dialog.title = "修改公司";
    state.dialog.submitTxt = "修 改";
  } else {
    state.dialog.title = "新增公司";
    state.dialog.submitTxt = "新 增";
    // 清空表单，此项需加表单验证才能使用
    // nextTick(() => {
    // 	userDialogFormRef.value.resetFields();
    // });
  }
  state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
  companyDialogFormRef.value.resetFields();
  state.dialog.isShowDialog = false;
  emit("refresh");
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = () => {
  if (state.dialog.type === "add") {
    add(
      state.ruleForm.name,
      state.ruleForm.money,
      state.ruleForm.address,
      state.ruleForm.tel,
      state.ruleForm.note
    ).then((res) => {
      if (res.code == 0) {
        ElMessage.success(res.msg);
        closeDialog();
      
      }
    });
  } else {
    update(
      state.ruleForm.id,
      state.ruleForm.name,
      state.ruleForm.money,
      state.ruleForm.address,
      state.ruleForm.tel,
      state.ruleForm.note
    ).then((res) => {
      if (res.code == 0) {
        ElMessage.success(res.msg);
        closeDialog();

      }
    });
  }
};

// 暴露变量
defineExpose({
  openDialog,
});
</script>
